home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / options.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  14.8 KB  |  426 lines

  1. ## system-config-printer
  2.  
  3. ## Copyright (C) 2006, 2007, 2008, 2009, 2010 Red Hat, Inc.
  4. ## Authors:
  5. ##  Tim Waugh <twaugh@redhat.com>
  6. ##  Florian Festi <ffesti@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import gobject
  23. import gtk
  24. import ppdippstr
  25.  
  26. def OptionWidget(name, v, s, on_change):
  27.     if isinstance(v, list):
  28.         # XXX
  29.         if isinstance(s, list):
  30.             for vv in v + s:
  31.                 if not isinstance(vv, str): raise ValueError
  32.             return OptionSelectMany(name, v, s, on_change)
  33.         print v, s
  34.         raise NotImplemented
  35.     else:
  36.         if (isinstance(s, int) or
  37.             isinstance(s, float) or
  38.             (isinstance(s, tuple) and
  39.              len(s) == 2 and
  40.              ((isinstance(s[0], int) and isinstance(s[1], int)) or
  41.               (isinstance(s[0], float) and isinstance(s[1], float))))):
  42.             try:
  43.                 if (isinstance(s, int) or
  44.                     isinstance(s, tuple) and isinstance(s[0], int)):
  45.                     v = int(v)
  46.                 else:
  47.                     v = float(v)
  48.             except ValueError:
  49.                 return OptionText(name, v, "", on_change)
  50.             return OptionNumeric(name, v, s, on_change)
  51.         elif isinstance(s, list):
  52.             for sv in s:
  53.                 if not isinstance(sv, int):
  54.                     return OptionSelectOne(name, v, s, on_change)
  55.             try:
  56.                 v = int(v)
  57.             except ValueError:
  58.                 return OptionSelectOne(name, v, s, on_change)
  59.             return OptionSelectOneNumber(name, v, s, on_change)
  60.         elif isinstance(s, str):
  61.             return OptionText(name, v, s, on_change)
  62.         else:
  63.             raise ValueError
  64.  
  65. # ---------------------------------------------------------------------------
  66.  
  67. class OptionInterface:
  68.     def get_default(self):
  69.         return None
  70.  
  71.     def get_current_value(self):
  72.         raise NotImplemented
  73.  
  74.     def is_changed(self):
  75.         raise NotImplemented
  76.  
  77. class OptionAlwaysShown(OptionInterface):
  78.     # States
  79.     STATE_UNCHANGED=0
  80.     STATE_RESET=1
  81.     STATE_ADJUSTED=2
  82.  
  83.     def __init__(self, name, ipp_type, system_default,
  84.                  widget, button, combobox_map = None, use_supported = False):
  85.         self.name = name
  86.         self.widget = widget
  87.         self.button = button
  88.         if ipp_type == bool:
  89.             def bool_type (x):
  90.                 if type (x) == str:
  91.                     if x.lower () in ("false", "no", "off"):
  92.                         return False
  93.                     # Even the empty string is true.
  94.                     return True
  95.                 return bool (x)
  96.             ipp_type = bool_type
  97.         self.ipp_type = ipp_type
  98.         self.set_default (system_default)
  99.         self.combobox_map = combobox_map
  100.  
  101.         if (type(self.widget) == gtk.ComboBox and
  102.             self.widget.get_model () == None):
  103.             print "No ComboBox model for %s" % self.name
  104.             model = gtk.ListStore (gobject.TYPE_STRING)
  105.             self.widget.set_model (model)
  106.  
  107.         if combobox_map != None and ipp_type == int:
  108.             model = self.widget.get_model ()
  109.             i = 0
  110.             dict = {}
  111.             iter = model.get_iter_first ()
  112.             while iter:
  113.                 dict[combobox_map[i]] = model.get_value (iter, 0)
  114.                 i += 1
  115.                 iter = model.iter_next (iter)
  116.             self.combobox_dict = dict
  117.         self.use_supported = use_supported
  118.         self.reinit (None)
  119.  
  120.     def get_default(self):
  121.         return self.system_default
  122.  
  123.     def set_default(self, system_default):
  124.         # For the media option, the system default depends on the printer's
  125.         # PageSize setting.  This method allows the main module to tell us
  126.         # what that is.
  127.         self.system_default = self.ipp_type (system_default)
  128.  
  129.     def reinit(self, original_value, supported=None):
  130.         """Set the original value of the option and the supported choices.
  131.         The special value None for original_value resets the option to the
  132.         system default."""
  133.         if (supported != None and
  134.             self.use_supported):
  135.             if (type(self.widget) == gtk.ComboBox and
  136.                 self.ipp_type == str):
  137.                 model = self.widget.get_model ()
  138.                 model.clear ()
  139.                 translations = ppdippstr.job_options.get (self.name)
  140.                 if translations:
  141.                     self.combobox_map = []
  142.                     self.combobox_dict = dict()
  143.                     i = 0
  144.  
  145.                 for each in supported:
  146.                     if translations:
  147.                         self.combobox_map.append (each)
  148.                         text = translations.get (each)
  149.                         self.combobox_dict[each] = text
  150.                         i += 1
  151.                     else:
  152.                         text = each
  153.  
  154.                     iter = model.append ()
  155.                     model.set_value (iter, 0, text)
  156.             elif (type(self.widget) == gtk.ComboBox and
  157.                   self.ipp_type == int and
  158.                   self.combobox_map != None):
  159.                 model = self.widget.get_model ()
  160.                 model.clear ()
  161.                 for each in supported:
  162.                     iter = model.append ()
  163.                     model.set_value (iter, 0, self.combobox_dict[each])
  164.  
  165.         if original_value != None:
  166.             self.original_value = self.ipp_type (original_value)
  167.             self.set_widget_value (self.original_value)
  168.             self.button.set_sensitive (True)
  169.         else:
  170.             self.original_value = None
  171.             self.set_widget_value (self.system_default)
  172.             self.button.set_sensitive (False)
  173.         self.state = self.STATE_UNCHANGED
  174.  
  175.     def set_widget_value(self, ipp_value):
  176.         t = type(self.widget)
  177.         if t == gtk.SpinButton:
  178.             return self.widget.set_value (ipp_value)
  179.         elif t == gtk.ComboBox:
  180.             if self.ipp_type == str and self.combobox_map == None:
  181.                 model = self.widget.get_model ()
  182.                 iter = model.get_iter_first ()
  183.                 while (iter != None and
  184.                        model.get_value (iter, 0) != ipp_value):
  185.                     iter = model.iter_next (iter)
  186.                 if iter:
  187.                     self.widget.set_active_iter (iter)
  188.             else:
  189.                 # It's an int.
  190.                 if self.combobox_map:
  191.                     index = self.combobox_map.index (ipp_value)
  192.                 else:
  193.                     index = ipp_value
  194.                 return self.widget.set_active (index)
  195.         elif t == gtk.CheckButton:
  196.             return self.widget.set_active (ipp_value)
  197.         else:
  198.             raise NotImplemented
  199.  
  200.     def get_widget_value(self):
  201.         t = type(self.widget)
  202.         if t == gtk.SpinButton:
  203.             # Ideally we would use self.widget.get_value() here, but
  204.             # it doesn't work if the value has been typed in and then
  205.             # the Apply button immediately clicked.  To handle this,
  206.             # we use self.widget.get_text() and fall back to
  207.             # get_value() if the result cannot be interpreted as the
  208.             # type we expect.
  209.             try:
  210.                 return self.ipp_type (self.widget.get_text ())
  211.             except ValueError:
  212.                 # Can't convert result of get_text() to ipp_type.
  213.                 return self.ipp_type (self.widget.get_value ())
  214.         elif t == gtk.ComboBox:
  215.             if self.combobox_map:
  216.                 return self.combobox_map[self.widget.get_active()]
  217.             if self.ipp_type == str:
  218.                 return self.widget.get_active_text ()
  219.             return self.ipp_type (self.widget.get_active ())
  220.         elif t == gtk.CheckButton:
  221.             return self.ipp_type (self.widget.get_active ())
  222.  
  223.         print t
  224.         raise NotImplemented
  225.  
  226.     def get_current_value(self):
  227.         return self.get_widget_value ()
  228.  
  229.     def is_changed(self):
  230.         if self.original_value != None:
  231.             # There was a value set previously.
  232.             if self.state == self.STATE_RESET:
  233.                 # It's been removed.
  234.                 return True
  235.             if self.state == self.STATE_ADJUSTED:
  236.                 if self.get_current_value () != self.original_value:
  237.                     return True
  238.                 return False
  239.  
  240.             # The value is the same as before, and not reset.
  241.             return False
  242.  
  243.         # There was no original value set.
  244.         if self.state == self.STATE_ADJUSTED:
  245.             # It's been adjusted.
  246.             return True
  247.  
  248.         # It's been left alone, or possible adjusted and then reset.
  249.         return False
  250.  
  251.     def reset(self):
  252.         self.set_widget_value (self.system_default)
  253.         self.state = self.STATE_RESET
  254.         self.button.set_sensitive (False)
  255.  
  256.     def changed(self):
  257.         self.state = self.STATE_ADJUSTED
  258.         self.button.set_sensitive (True)
  259.  
  260. class OptionAlwaysShownSpecial(OptionAlwaysShown):
  261.     def __init__(self, name, ipp_type, system_default,
  262.                  widget, button, combobox_map = None, use_supported = False,
  263.                  special_choice = "System default"):
  264.         self.special_choice = special_choice
  265.         self.special_choice_shown = False
  266.         OptionAlwaysShown.__init__ (self, name, ipp_type, system_default,
  267.                                     widget, button,
  268.                                     combobox_map=combobox_map,
  269.                                     use_supported=use_supported)
  270.  
  271.     def show_special_choice (self):
  272.         if self.special_choice_shown:
  273.             return
  274.  
  275.         self.special_choice_shown = True
  276.         # Only works for ComboBox widgets
  277.         model = self.widget.get_model ()
  278.         iter = model.insert (0)
  279.         model.set_value (iter, 0, self.special_choice)
  280.         self.widget.set_active_iter (model.get_iter_first ())
  281.  
  282.     def hide_special_choice (self):
  283.         if not self.special_choice_shown:
  284.             return
  285.  
  286.         self.special_choice_shown = False
  287.         # Only works for ComboBox widgets
  288.         model = self.widget.get_model ()
  289.         model.remove (model.get_iter_first ())
  290.  
  291.     def reinit(self, original_value, supported=None):
  292.         if original_value != None:
  293.             self.hide_special_choice ()
  294.         else:
  295.             self.show_special_choice ()
  296.  
  297.         OptionAlwaysShown.reinit (self, original_value, supported=supported)
  298.  
  299.     def reset(self):
  300.         self.show_special_choice ()
  301.         OptionAlwaysShown.reset (self)
  302.  
  303.     def changed(self):
  304.         OptionAlwaysShown.changed (self)
  305.         if self.widget.get_active () > 0:
  306.             self.hide_special_choice ()
  307.  
  308. class Option(OptionInterface):
  309.  
  310.     conflicts = None
  311.  
  312.     def __init__(self, name, value, supported, on_change):
  313.         self.name = name
  314.         self.value = value
  315.         self.supported = supported
  316.         self.on_change = on_change
  317.         self.is_new = False
  318.  
  319.         label = name
  320.         if not label.endswith (':'):
  321.             label += ':'
  322.         self.label = gtk.Label(label)
  323.         self.label.set_alignment(0.0, 0.5)
  324.  
  325.     def get_current_value(self):
  326.         raise NotImplemented
  327.  
  328.     def is_changed(self):
  329.         return (self.is_new or
  330.                 str (self.get_current_value()) != str (self.value))
  331.  
  332.     def changed(self, widget, *args):
  333.         self.on_change(self)
  334.     
  335. # ---------------------------------------------------------------------------
  336.  
  337. class OptionSelectOne(Option):
  338.  
  339.     def __init__(self, name, value, supported, on_change):
  340.         Option.__init__(self, name, value, supported, on_change)
  341.  
  342.         self.selector = gtk.combo_box_new_text()
  343.         
  344.         
  345.         selected = None
  346.         for nr, choice in enumerate(supported):
  347.             self.selector.append_text(str(choice))
  348.             if str (value) == str (choice):
  349.                 selected = nr
  350.         if selected is not None:
  351.             self.selector.set_active(selected)
  352.         else:
  353.             print "Unknown value for %s: %s" % (name, value)
  354.             print "Choices:", supported
  355.         self.selector.connect("changed", self.changed)
  356.  
  357.     def get_current_value(self):
  358.         return self.selector.get_active_text()
  359.  
  360. # ---------------------------------------------------------------------------
  361.  
  362. class OptionSelectOneNumber(OptionSelectOne):
  363.  
  364.     def get_current_value(self):
  365.         return int(self.selector.get_active_text())
  366.  
  367. # ---------------------------------------------------------------------------
  368.  
  369. class OptionSelectMany(Option):
  370.  
  371.     def __init__(self, name, value, supported, on_change):
  372.         Option.__init__(self, name, value, supported, on_change)
  373.         self.checkboxes = []
  374.         vbox = gtk.VBox()
  375.  
  376.         for s in supported:
  377.             checkbox = gtk.CheckButton(label=s)
  378.             checkbox.set_active(s in value)
  379.             vbox.add(checkbox)
  380.             checkbox.connect("toggled", self.changed)
  381.             self.checkboxes.append(checkbox)
  382.         self.selector = vbox
  383.             
  384.     def get_current_value(self):
  385.         return[s for s, chk in zip(self.supported, self.checkboxes)
  386.                if chk.get_active()]
  387.  
  388. # ---------------------------------------------------------------------------
  389.  
  390. class OptionNumeric(Option):
  391.     def __init__(self, name, value, supported, on_change):
  392.         self.is_float = (isinstance(supported, float) or
  393.                          (isinstance(supported, tuple) and
  394.                           isinstance(supported[0], float)))
  395.         if self.is_float:
  396.             digits = 2
  397.         else:
  398.             digits = 0
  399.  
  400.         if not isinstance(supported, tuple):
  401.             supported = (0, supported)
  402.         Option.__init__(self, name, value, supported, on_change)
  403.         adj = gtk.Adjustment(value, supported[0], supported[1], 1.0, 5.0, 0.0)
  404.         self.selector = gtk.SpinButton(adj, climb_rate=1.0, digits=digits)
  405.         if not self.is_float:
  406.             self.selector.set_numeric(True)
  407.         self.selector.connect("changed", self.changed)
  408.  
  409.     def get_current_value(self):
  410.         if self.is_float:
  411.             return self.selector.get_value()
  412.         return self.selector.get_value_as_int()
  413.  
  414. # ---------------------------------------------------------------------------
  415.  
  416. class OptionText(Option):
  417.     def __init__(self, name, value, supported, on_change):
  418.         Option.__init__(self, name, value, supported, on_change)
  419.  
  420.         self.selector = gtk.Entry()
  421.         self.selector.set_text(value)
  422.         self.selector.connect("changed", self.changed)
  423.  
  424.     def get_current_value(self):
  425.         return self.selector.get_text()
  426.